
function homeBoutIndexCell(cell, cellData, rowData, rowIndex, colIndex) {
    $(cell).addClass(rowData.home.style);
}
function awayBoutIndexCell(cell, cellData, rowData, rowIndex, colIndex) {
    $(cell).addClass(rowData.away.style);
}

function createBoutsIndex() {
    var boutList = [];
    $(DXML.boutIndex).each(function(index, value) {
        var homePts = Number($(value).attr("home-score"));
        var awayPts = Number($(value).attr("away-score"));
        
        var boutInfo = {
            kind: $(value).attr("type") || "",
            date: $(value).attr("date") || "",
            name: $(value).attr("name") || "",
            url: $(value).attr("href"),
            home: {
                name: $(value).attr("home-name") || "Home",
                points: homePts,
                style: homePts > awayPts ? "win" : "loss"
            },
            away: {
                name: $(value).attr("away-name") || "Away",
                points: awayPts,
                style: awayPts > homePts ? "win" : "loss"
            }
        }
        boutList.push(boutInfo);
    });
//    Tempo.prepare($("#boutsIndex")).render(boutList);
//    $(".boutrow").click( function() {
//      ReloadBoutData("DerbyXMLFiles/"+this.getAttribute("bouturl")+".derbyxml");
//    });
    var tableB = $("#boutsTable").DataTable({
        paging: false,
        searching: false,
        info: false,
        data: boutList,
        order: [0, 'desc'],
        columns: [
            {title:"Date", data: "date", type:'date'},
            {title:"Kind", data: "kind"},
            {title:"Name", data: "name"},
            {title:"Home", data: "home.name", createdCell: homeBoutIndexCell },
            {title:"Score", data: "home.points", createdCell: homeBoutIndexCell},
            {title:"Visitor", data: "away.name", createdCell: awayBoutIndexCell},
            {title:"Score", data: "away.points", createdCell: awayBoutIndexCell},
        ],
        createdRow: function( row, data, dataIndex ) {
            $(row).addClass("boutrow");
            $(row).click( function() {
              ReloadBoutData("DerbyXMLFiles/"+data.url+".derbyxml");
            });
        }
    });
    
}
function  createTeamsIndex() {
}
function createSkatersIndex() {
}

function loadLeagueXML(document) {
    var dxml = document.firstChild;
    $(dxml).find('league').each(function (index, leagueXML) {
        var teamIcons = DXML.leagueIcons[$(leagueXML).attr("name")];
        if (!teamIcons) {
            teamIcons = {};
            DXML.leagueIcons[$(leagueXML).attr("name")] = teamIcons;
        }
        var teamColors = DXML.leagueColors[$(leagueXML).attr("name")];
        if (!teamColors) {
            teamColors = {};
            DXML.leagueColors[$(leagueXML).attr("name")] = teamColors;
        }
        $(leagueXML).find('team').each(function (index, teamXML) {
            teamIcons[$(teamXML).attr("name")] = iconInformationFromTeam(teamXML);
            teamColors[$(teamXML).attr("name")] = colorInformationFromTeam(teamXML);
        });
    });
    DXML.boutIndex = $(dxml).find('bout');
//    DXML.skaterIndex = $(dxml).find('person');
    if ($("#boutsIndex").length) {
        createBoutsIndex();
    }
    if ($("#teamsIndex").length) {
        createTeamsIndex();
    }
    if ($("#skatersIndex").length) {
        createSkatersIndex();
    }
}
